package com.jlastu.androidsx.ui.merchant;

import android.Manifest;
import android.view.KeyEvent;
import android.widget.RadioGroup;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import com.amap.api.location.AMapLocation;
import com.jlastu.androidsx.R;
import com.jlastu.androidsx.adapter.MainPagerAdapter;
import com.jlastu.androidsx.databinding.ActivityMerchantMainBinding;
import com.jlastu.androidsx.fragment.MUserFragment;
import com.jlastu.androidsx.fragment.MerchantFragment;
import com.jlastu.androidsx.fragment.MerchantHomeFragment;
import com.jlastu.androidsx.utils.LocalUtil;
import com.jlastu.androidsx.utils.Utils;
import com.huantansheng.easyphotos.EasyPhotos;
import com.huantansheng.easyphotos.utils.permission.PermissionUtil;

import java.util.ArrayList;
import java.util.List;

import cn.project.base.baseui.BaseNormalActivity;

public class MerchantMainActivity extends BaseNormalActivity<ActivityMerchantMainBinding> implements RadioGroup.OnCheckedChangeListener {

    private long clickTime;

    boolean isGet = false;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_merchant_main;
    }

    @Override
    protected void initView() {

        mBinding.radioGroup.setOnCheckedChangeListener(this);

    }

    @Override
    protected void initData() {
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new MerchantHomeFragment());
        fragments.add(new MerchantFragment());
        MainPagerAdapter adapter = new MainPagerAdapter(getSupportFragmentManager(), fragments);
        mBinding.viwepager.setOffscreenPageLimit(2);
        mBinding.viwepager.setAdapter(adapter);

        String[] permissions = new String[]{Manifest.permission.ACCESS_FINE_LOCATION, Manifest.permission.ACCESS_COARSE_LOCATION};
        // 检查并请求权限
        if (PermissionUtil.checkAndRequestPermissionsInActivity(this,
                permissions)) {
            //获取位置数据
            LocalUtil localUtil = new LocalUtil(getApplicationContext());
            localUtil.startLocation();
            localUtil.setOnItemClickListener(new LocalUtil.OnItemClickListener() {
                @Override
                public void onItemClick(double longitude, double latitude, AMapLocation location, String count, String address) {
                    if (!isGet){
                        isGet = true;
                        Utils.ADDRESS = location.getProvince()+"-"+location.getDistrict();
                        Utils.DETAIL_ADDRESS = location.getAddress();
                    }
                }
            });
        }
    }

    @Override
    public void onCheckedChanged(RadioGroup group, int checkedId) {
        switch (checkedId) {
            case R.id.tab_home:
                mBinding.viwepager.setCurrentItem(0, true);
                break;
            case R.id.tab_me:
                mBinding.viwepager.setCurrentItem(1, true);
                break;
        }
    }


    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            exit();
            return true;
        } else {
            return super.onKeyDown(keyCode, event);
        }
    }

    private void exit() {
        if ((System.currentTimeMillis() - clickTime) > 2000) {
            Toast.makeText(this, "再按一次后退键退出程序", Toast.LENGTH_SHORT).show();
            clickTime = System.currentTimeMillis();
        } else {
            try {
                //正常退出
                android.os.Process.killProcess(android.os.Process.myPid());
                System.exit(0);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }

    /**
     * 预加载相册扫描，可以增加点速度，写不写都行
     * 该方法如果没有授权读取权限的话，是无效的，所以外部加不加权限控制都可以，加的话保证执行，不加也不影响程序正常使用。
     */
    private void preLoadAlbums() {
        EasyPhotos.preLoad(MerchantMainActivity.this);
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull final String[] permissions,
                                           @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);

        PermissionUtil.onPermissionResult(MerchantMainActivity.this, permissions, grantResults,
                new PermissionUtil.PermissionCallBack() {
                    @Override
                    public void onSuccess() {
                        preLoadAlbums();
                    }

                    @Override
                    public void onShouldShow() {
                    }

                    @Override
                    public void onFailed() {
                    }
                });
    }
}


